<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-05
 * Time: 16:38
 */

namespace app\api\controller\v1;


use app\api\model\HotelRoom as HotelRoomModel;
use app\api\model\HotelRoomOrder;
use app\api\model\Image;
use app\api\model\LinUser;
use app\api\service\order\RoomOrderCheckSer;
use app\api\service\room\CreateRoom;
use app\api\service\room\UpdateRoom;
use LinCmsTp5\exception\ParameterException;
use think\Db;
use think\facade\Request;

class HotelRoom
{
    /**
     * 获取列表
     * @validate('PagingParameter')
     * @auth('酒店房间','酒店信息设置')
     */
    public function getDataList($page,$limit,$name,$lin_user_id,$province_id,$city_id,$county_id)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        $linUser = LinUser::get($lin_user_id);
        if($lin_user_id > 1) {
            if( $linUser['group_id'] != 3) {
                $where[] = ['lin_user_id','eq',$lin_user_id];
            }
        }
        if($province_id) {
            $where[] = ['province_id','eq',$province_id];
        }
        if($city_id) {
            $where[] =['city_id','eq',$city_id];
        }
        if($county_id) {
            $where[] = ['county_id','eq',$county_id];
        }
        $query = HotelRoomModel::where($where)->with('adminUser')
            ->order('hotel_room.id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['recommend'] == 1) {
                $item['recommend_name'] = '不推荐首页';
            } else {
                $item['recommend_name'] = '推荐首页';
            }
            $item['order_num'] = HotelRoomOrder::where('hotel_room_id',$item['id'])->where('status','gt',1)->count();

        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return HotelRoomModel::with('imgs.img,basis,tag,facility,claim')->where('id',$id)->find();
    }

    /**
     * 创建分类
     * @validate('CheckHotel')
     */
    public function createData()
    {
        $data = Request::post();
        $createRoom = new CreateRoom($data);
        return $createRoom->create();
    }

    /**
     * 更新信息
     * @validate('CheckHotel')
     */
    public function updateData()
    {
        $data = Request::put();
        $updateRoom = new UpdateRoom($data);
        return $updateRoom->update();
    }

    /**
     * 修改首页推荐
     */
    public function updateDataOne()
    {
        $data = Request::put();
        $mode = new HotelRoomModel();
        $mode->allowField(true)->save($data,['id',$data['id']]);
        return writeJson(202, [], '修改成功！');
    }

    /**
     * 删除信息
     * @validate('IdMustBePositiveInt')
     */
    public function deleteData($id)
    {
        HotelRoomModel::destroy($id);
        return writeJson(203, [], '删除成功！');
    }

    /**
     * 获取订单列表
     *  @validate('PagingParameter')
     * @auth('酒店订单','酒店信息设置')
     */
    public function getOrderDataList($page,$limit,$name,$phone,$id_no,$status,$lin_user_id)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        $linUser = LinUser::get($lin_user_id);
        if($lin_user_id > 1) {
            if( $linUser['group_id'] != 4) {
                $where[] = ['lin_user_id','eq',$lin_user_id];
            }
        }
        if($phone) {
            $where[] = ['phone','like','%'.$phone.'%'];
        }
        if($phone) {
            $where[] = ['id_no','like','%'.$id_no.'%'];
        }
        if($status) {
            $where[] = ['status','=',$status];
        } else {
            $where[] = ['status','>',1];
        }
        $query = HotelRoomOrder::where($where)->with('adminUser')->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 2) {
                $item['status_name'] = '已订房';
            } elseif($item['status'] == 3) {
                $item['status_name'] = '未入住申请退款';
            } elseif($item['status'] == 4) {
                $item['status_name'] = '已入住';
            } elseif($item['status'] == 5) {
                $item['status_name'] = '已入住未退款';
            } elseif($item['status'] == 6) {
                $item['status_name'] = '未入住已退款';
            } elseif($item['status'] == 7) {
                $item['status_name'] = '已退房';
            }
            if($item['pay_time']) {
                $item['pay_time_name'] = date('Y-m-d H:i:s',$item['pay_time']);
            } else {
                $item['pay_time_name'] = '';
            }
            if($item['refund_time']) {
                $item['refund_time_name'] = date('Y-m-d H:i:s',$item['refund_time']);
            } else {
                $item['refund_time_name'] = '';
            }
            if($item['send_time']) {
                $item['send_time_name'] = date('Y-m-d H:i:s',$item['send_time']);
            } else {
                $item['send_time_name'] = '';
            }
            if($item['refund_imgs']) {
                $imgs = explode('-',$item['refund_imgs']);
                $item['refund_imgs'] = Image::where('id','in',$imgs)->select()->toArray();
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 后台点击入住
     */
    public function roomOrderCheck($order_id,$lin_user_id)
    {
        $roomOrderSer = new RoomOrderCheckSer($order_id,$lin_user_id);
        return $roomOrderSer->entrance();
    }

    /**
     * 后台点击退房
     */
    public function roomOrderSend($order_id,$refund_remark,$refund_imgs)
    {
        HotelRoomOrder::where('id',$order_id)->update([
            'refund_remark' => $refund_remark,
            'refund_imgs' => implode('-',$refund_imgs),
            'status' => 5
        ]);
        return writeJson(201, [], '操作成功！');
    }

    /**
     * 后台点击退房
     */
    public function roomOrderSendOne($order_id,$lin_user_id,$price)
    {
        if(!$lin_user_id) {
            $lin_user_id = 0;
        }
        $data =  json_decode(https_post("https://youju.simexcn.com/api/v1/pay/admin/refund",[
            'order_id' => $order_id,
            'lin_user_id' => $lin_user_id,
            'price' => $price,
            'string' => 'N#l0owmG82aDGxH'
        ]),true);
        if($data['error_code'] != 201) {
            throw new ParameterException([
                'msg' => [
                    'hotel_order' => $data['msg']['room_order']
                ]
            ]);
        } else {
            return $data;
        }
    }
}